package com.hetao.xx.service;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.hetao.xx.game.model.User;
import com.hetao.xx.Global;
import com.hetao.xx.game.model.UserArms;
import com.hetao.xx.orm.UserVo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UserManager {


    public static UserVo loadUserInfo(String name) {
        return Global.getInstant().userDao.queryByName(name);
    }

    public static int saveUserInfo(UserVo userVo) {
        if(userVo.getId() == 0) {
            return Global.getInstant().userDao.add(userVo);
        }

        return Global.getInstant().userDao.update(userVo);
    }

    public static List<UserVo> loadUserList() {
        return Global.getInstant().userDao.queryAll();
    }


    public static boolean checkUser(String name) {
        if(TextUtils.isEmpty(name)) {
            Global.getInstant().toastMsg("名字不能为空");
            return false;
        }

        UserVo userVo = Global.getInstant().userDao.queryByName(name);
        if(userVo != null) {
            Global.getInstant().toastMsg(name+"的文档已存在，请另起大名");
            return false;
        }

        return true;
    }

    public static int delUser(UserVo userVo) {
        return Global.getInstant().userDao.delById(userVo.getId());
    }

    /**
     * 读取用户装备
     * @param userVo
     * @return
     */
    public static User fullUserArms(UserVo userVo) {
        User user = new User();
        user.info = userVo;
        user.amrs = new UserArms();
        return user;
    }

    /**
     * 存档
     */
    public static void saveAll() {
        User user = Global.getInstant().getUser();
        saveUserInfo(user.info);
    }

}
